﻿Imports Telerik.Web.UI

Partial Class EmployeeTVB
    Inherits System.Web.UI.Page
    Dim _CommandName As String

    Dim Form As UserControl
    Dim Grid As RadGrid

    Dim EmpObj As New DBEmployeeSP
    Dim EmpDatatset As New Data.DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            'Page.Header.DataBind()
            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")

            rtMain.FindItemByValue("Edit").Enabled = False
            rtMain.FindItemByValue("Save").Enabled = False
            rtMain.FindItemByValue("Delete").Enabled = False
            rtMain.FindItemByValue("Cancel").Enabled = False

        End If

    End Sub


    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        'Dim rtMain As RadToolBar = Page.Master.FindControl("rtMain")

        'If Not Page.IsPostBack Then
        '    Dim AjaxManager As RadAjaxManager = RadAjaxManager.GetCurrent(Page)
        '    'Dim ToolBar As RadToolBar = CType(Content("rtMain"), RadToolBar)
        '    AjaxManager.AjaxSettings.AddAjaxSetting(rgEmployee, rtMain, Nothing)
        'End If

        'Dim obj As New Authorization
        'Dim url As String = Request.AppRelativeCurrentExecutionFilePath

        'Dim id As String = obj.GetMenuID(url)
        'Dim is_Authorized As Boolean = obj.CheckAuthorization(id)

        'If is_Authorized = True Then
        '    'User Authorized to See that page .. Continue
        'Else
        '    'User is not Authorized 
        '    Response.Redirect("~/UnAuthorized.aspx")
        'End If


    End Sub



    'Protected Sub RadAjaxManager_AjaxSettingCreating(ByVal sender As Object, ByVal e As AjaxSettingCreatingEventArgs) Handles RadAjaxManager1.AjaxSettingCreating

    '    Dim rtMain As RadToolBar = Page.Master.FindControl("rtMain")

    '    e.UpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional

    '    If (e.Initiator.ID.IndexOf("rgEmployee") > -1) Then
    '        'e.UpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional
    '        e.Updated = rtMain
    '    End If

    'End Sub



    Protected Sub rgEmployee_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgEmployee.ItemCommand
        _CommandName = e.CommandName
    End Sub



    Protected Sub rgEmployee_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rgEmployee.SelectedIndexChanged

        'Dim rtMain As RadToolBar = Me.Master.FindControl("rtMain")

        For Each item As GridDataItem In rgEmployee.SelectedItems
            item = DirectCast(rgEmployee.SelectedItems(0), GridDataItem)
            'access the value here
            CType(EmployeesForm.FindControl("rtbID"), RadTextBox).Text = item("Emp_ID").Text.ToString()
            Session("EmployeeID") = item("Emp_ID").Text.ToString()
        Next

        'Session("EmployeeID") = rgEmployee.
        rtMain.FindItemByValue("Edit").Enabled = True
        rtMain.FindItemByValue("Delete").Enabled = True


    End Sub




    Protected Sub rgEmployee_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgEmployee.ItemDataBound


        If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) And _CommandName <> "InitInsert" Then
            'If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then


            Dim editFormItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
            Dim parentItem As GridDataItem = editFormItem.ParentItem

            'Dim ddl As DropDownList = DirectCast(parentItem("CompanyGroup").Controls(0), DropDownList)

            Dim box As RadComboBox = CType(editFormItem.FindControl("rcbCompanyGroup").FindControl("RadComboBoxDescription"), RadComboBox)
            box.SelectedValue = parentItem("CompanyGroup_ID").Text


            'Dim editFormItem2 As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)

            'Dim ddl As DropDownList = DirectCast(editFormItem2.FindControl("CompanyGroup"), DropDownList)

            'Dim value As String = ddl.SelectedValue


            Dim box1 As RadComboBox = CType(editFormItem.FindControl("rcbCompany").FindControl("RadComboBoxDescription"), RadComboBox)
            box1.SelectedValue = parentItem("Company_ID").Text

            Dim box2 As RadComboBox = CType(editFormItem.FindControl("rcbEmp_Type").FindControl("RadComboBoxDescription"), RadComboBox)
            box2.SelectedValue = parentItem("Emp_Type_ID").Text
            Dim box3 As RadComboBox = CType(editFormItem.FindControl("rcbDepartment").FindControl("RadComboBoxDescription"), RadComboBox)
            box3.SelectedValue = parentItem("Department_ID").Text

            Dim box4 As RadComboBox = CType(editFormItem.FindControl("rcbGender").FindControl("RadComboBoxDescription"), RadComboBox)
            box4.SelectedValue = parentItem("Gender_ID").Text
            Dim box5 As RadComboBox = CType(editFormItem.FindControl("rcbProject").FindControl("RadComboBoxDescription"), RadComboBox)
            box5.SelectedValue = parentItem("Project_ID").Text


            Dim box6 As RadComboBox = CType(editFormItem.FindControl("rcbDesignation").FindControl("RadComboBoxDescription"), RadComboBox)
            Dim box7 As RadComboBox = CType(editFormItem.FindControl("rcbEmp_Status").FindControl("RadComboBoxDescription"), RadComboBox)

            box6.SelectedValue = parentItem("Designation_ID").Text
            box7.SelectedValue = parentItem("Emp_Status_ID").Text

            '' '' ''Dim chkBox As CheckBox = CType(editFormItem.FindControl("chkEdtIsComplete"), CheckBox)
            '' '' ''chkBox.Checked = parentItem("IsComplete").Text


        End If

    End Sub








    Protected Sub rtMain_ButtonClick(ByVal sender As Object, ByVal e As RadToolBarEventArgs) Handles rtMain.ButtonClick

        Dim url As String = Request.AppRelativeCurrentExecutionFilePath

        If url.EndsWith("EmployeeTVB.aspx") = True Then
            Form = EmployeesForm
            Grid = rgEmployee
        End If

        If (e.Item.Value = "AddNew") Then

            Grid.Visible = False
            Form.Visible = True

            CType(Form.FindControl("rtbID"), RadTextBox).Text = ""

            rtMain.FindItemByValue("Save").Enabled = True
            rtMain.FindItemByValue("Cancel").Enabled = True

        ElseIf (e.Item.Value = "Cancel") Then
            ResetControls()

            Form.Visible = False
            'Grid.Rebind()
            Grid.Visible = True
        ElseIf (e.Item.Value = "Edit") Then
            LoadControls()
            rtMain.FindItemByValue("Save").Enabled = True
            rtMain.FindItemByValue("Cancel").Enabled = True
            Form.Visible = True
            Grid.Visible = False
        ElseIf (e.Item.Value = "Save") Then
            SaveControls()
            ResetControls()
            Form.Visible = False
            Grid.Rebind()
            Grid.Visible = True
        ElseIf (e.Item.Value = "Delete") Then

            DeleteRecord()
            Grid.Rebind()
            Grid.Visible = True
            'PrintRadGrid
        ElseIf (e.Item.Value = "Print") Then
            'RadAjaxManager1.ResponseScripts.Add("PrintRadGrid();")
        ElseIf (e.Item.Value = "pdf") Then

            Grid.ExportSettings.ExportOnlyData = True
            Grid.ExportSettings.IgnorePaging = True
            Grid.ExportSettings.OpenInNewWindow = True
            Grid.MasterTableView.ExportToPdf()

        ElseIf (e.Item.Value = "word") Then
            Grid.ExportSettings.ExportOnlyData = True
            Grid.ExportSettings.IgnorePaging = True
            Grid.MasterTableView.ExportToWord()

        ElseIf (e.Item.Value = "excel") Then
            Grid.ExportSettings.ExportOnlyData = True
            Grid.ExportSettings.IgnorePaging = True
            Grid.MasterTableView.ExportToExcel()

        ElseIf (e.Item.Value = "csv") Then
            Grid.ExportSettings.ExportOnlyData = True
            Grid.ExportSettings.IgnorePaging = True
            Grid.MasterTableView.ExportToCSV()

        ElseIf (e.Item.Value = "Refresh") Then
            If Grid.Visible = True Then
                Grid.Rebind()
            Else
                rtMain.FindItemByValue("Save").Enabled = True
                rtMain.FindItemByValue("Cancel").Enabled = True
            End If
        End If

    End Sub



#Region "Toolbar Functions"


    Sub ResetControls()


        Form = EmployeesForm
        Grid = rgEmployee

        Dim x As Integer = 0

        Dim ctr As Control

        For Each ctr In Form.Controls

            If (ctr.[GetType]().ToString() = "Telerik.Web.UI.RadTextBox") Then
                CType(ctr, RadTextBox).Text = ""
            ElseIf (ctr.[GetType]().ToString() = "ASP.usercontrols_onefiltereddropdownlist_ascx") Then
                CType(ctr.FindControl("RadComboBoxDescription"), Telerik.Web.UI.RadComboBox).SelectedIndex = 0
            ElseIf (ctr.[GetType]().ToString() = "Telerik.Web.UI.RadComboBox") Then
                CType(ctr, RadComboBox).SelectedIndex = 0
            ElseIf (ctr.[GetType]().ToString() = "Telerik.Web.UI.RadDatePicker") Then
                CType(ctr, RadDatePicker).DbSelectedDate = DBNull.Value
            End If

        Next


    End Sub


    Sub SaveControls()

        Form = EmployeesForm
        Grid = rgEmployee

        Dim Emp_ID As Integer
        If (Not (CType(Form.FindControl("rtbID"), RadTextBox).Text) Is Nothing And Not (CType(Form.FindControl("rtbID"), RadTextBox).Text) = "") Then
            Emp_ID = CType(CType(Form.FindControl("rtbID"), RadTextBox).Text.ToString(), Integer)
        End If

        Dim emp_Num As Integer
        If (Not (CType(Form.FindControl("rtbemp_Num"), RadTextBox).Text) Is Nothing And Not (CType(Form.FindControl("rtbemp_Num"), RadTextBox).Text) = "") Then
            emp_Num = CType(CType(Form.FindControl("rtbemp_Num"), RadTextBox).Text.ToString(), Integer)
        End If

        Dim FName As String = CType(Form.FindControl("rtbFName"), RadTextBox).Text.ToString()
        Dim LName As String = CType(Form.FindControl("rtbLName"), RadTextBox).Text.ToString()
        Dim CompanyGroup As Integer = CType(CType(Form.FindControl("rcbCompanyGroup").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue.ToString(), Integer)
        Dim Company As Integer = CType(CType(Form.FindControl("rcbCompany").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue.ToString(), Integer)
        Dim Emp_Type As Integer = CType(CType(Form.FindControl("rcbEmp_Type").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue.ToString(), Integer)
        Dim Department As Integer = CType(CType(Form.FindControl("rcbDepartment").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue.ToString(), Integer)
        Dim Gender As Integer = CType(CType(Form.FindControl("rcbGender").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue.ToString(), Integer)
        Dim Project As Integer = CType(CType(Form.FindControl("rcbProject").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue.ToString(), Integer)
        Dim Designation As Integer = CType(CType(Form.FindControl("rcbDesignation").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue.ToString(), Integer)
        Dim Emp_Status As Integer = CType(CType(Form.FindControl("rcbEmp_Status").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue.ToString(), Integer)

        Dim DateOfBirth As Date
        If Not (CType(Form.FindControl("rdpDateOfBirth"), Object)).ToString() Is Nothing Then
            DateOfBirth = CType(CType(Form.FindControl("rdpDateOfBirth"), RadDatePicker).DbSelectedDate, DateTime)
            'If DateOfBirth.ToString().Contains(":") = True Then
            '    DateBirth = DateOfBirth.ToShortDateString
            'End If
        End If

        EmpDatatset = EmpObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "Save&Update_Employee", Emp_ID, emp_Num, FName, LName, Emp_Type, Gender, DateOfBirth, Designation, CompanyGroup, Company, Department, Project, Emp_Status, )



    End Sub

    Sub DeleteRecord()

        Dim url As String = Request.AppRelativeCurrentExecutionFilePath


            Dim Emp_ID As String = Session("EmployeeID").ToString()

        EmpDatatset = EmpObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "deleteEmployee", Emp_ID, , , , , , , , , , , , , )

            Session("EmployeeID") = Nothing


    End Sub



    Sub LoadControls()


            Form = EmployeesForm
            Grid = rgEmployee

            EmpDatatset = EmpObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvOneEmployee", CType(CType(Form.FindControl("rtbID"), RadTextBox).Text.ToString(), Integer), , , , , , , , , , , , , )

        CType(Form.FindControl("rcbCompanyGroup").FindControl("RadComboBoxDescription"), RadComboBox).EnableViewState = False

            CType(Form.FindControl("rtbID"), RadTextBox).Text = EmpDatatset.Tables(0).Rows(0).Item("Emp_ID").ToString()
            CType(Form.FindControl("rtbemp_Num"), RadTextBox).Text = EmpDatatset.Tables(0).Rows(0).Item("emp_Num").ToString()
            CType(Form.FindControl("rtbFName"), RadTextBox).Text = EmpDatatset.Tables(0).Rows(0).Item("FName").ToString()
            CType(Form.FindControl("rtbLName"), RadTextBox).Text = EmpDatatset.Tables(0).Rows(0).Item("LName").ToString()
            CType(Form.FindControl("rcbCompanyGroup").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue = EmpDatatset.Tables(0).Rows(0).Item("CompanyGroup").ToString()
            CType(Form.FindControl("rcbCompany").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue = EmpDatatset.Tables(0).Rows(0).Item("Company").ToString()
            CType(Form.FindControl("rcbEmp_Type").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue = EmpDatatset.Tables(0).Rows(0).Item("Emp_Type").ToString()
            CType(Form.FindControl("rcbDepartment").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue = EmpDatatset.Tables(0).Rows(0).Item("Department").ToString()
            CType(Form.FindControl("rcbGender").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue = EmpDatatset.Tables(0).Rows(0).Item("Gender").ToString()
            CType(Form.FindControl("rcbProject").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue = EmpDatatset.Tables(0).Rows(0).Item("Project").ToString()
            CType(Form.FindControl("rcbDesignation").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue = EmpDatatset.Tables(0).Rows(0).Item("Designation").ToString()
            CType(Form.FindControl("rcbEmp_Status").FindControl("RadComboBoxDescription"), RadComboBox).SelectedValue = EmpDatatset.Tables(0).Rows(0).Item("Emp_Status").ToString()

            If (Not ((EmpDatatset.Tables(0).Rows(0).Item("DateOfBirth").ToString()) = "") And (EmpDatatset.Tables(0).Rows(0).Item("DateOfBirth").ToString()) IsNot Nothing) Then
                CType(Form.FindControl("rdpDateOfBirth"), RadDatePicker).DbSelectedDate = CType(EmpDatatset.Tables(0).Rows(0).Item("DateOfBirth").ToString(), DateTime)
            End If

    End Sub



#End Region









End Class
